home *** CD-ROM | disk | FTP | other *** search
/ The Fatted Calf / The Fatted Calf.iso / Applications / GraphicViewers / pCD / Source / ImageView.m < prev    next >
Text File  |  1993-01-16  |  4KB  |  186 lines

  1. #import <appkit/appkit.h>
  2. // #import <dpsclient/psops.h>
  3. // #import <dpsclient/wraps.h>
  4.  
  5. #import "ImageView.h"
  6. #import "PhotoWinProcs.h"
  7. #import "Controller.h"        // for nextPasteCounter...
  8.  
  9. @implementation ImageView
  10.  
  11. - initForImage:(id)img :(const NXRect *)rect;
  12. {
  13.     NXSize  sz;
  14.     
  15.     self = [super initFrame:rect];
  16.     
  17.     theImage = img;
  18.     sz.width = [theImage pixelsWide];
  19.     sz.height = [theImage pixelsHigh];
  20.     [self sizeTo:sz.width :sz.height];
  21.     selectRect.size.width = 0; selectRect.size.height = 0;
  22.     
  23.     return self;
  24. }
  25.  
  26. - (BOOL)acceptsFirstResponder
  27. {
  28.     return YES;
  29. }
  30.  
  31. - (BOOL)acceptsFirstMouse
  32. {
  33.     return YES;
  34. }
  35.  
  36. - (id) image
  37. {
  38.     return theImage;
  39. }
  40.     
  41. - drawSelf:(const NXRect *)rects :(int)rectCount
  42. {
  43.     [theImage draw];
  44.     return self;
  45. }
  46.  
  47.  
  48.  
  49. #define DRAG_MASK (NX_MOUSEUPMASK|NX_MOUSEDRAGGEDMASK)
  50. - mouseDown:(NXEvent *)event
  51. {
  52.     NXPoint    p, start, last, maxBound;
  53.     NXSize    imSize, imPixelSize;
  54.     static const NXPoint hot = {7., 7.};
  55.     int        oldMask;
  56.     BOOL    doAlternateDrag = NO;
  57.     NXCoord    faceTiffSize = 64.0, altDragSize = 0;
  58.     static id    crossCursor = nil;
  59.     
  60.     [theImage getSize:&imSize];
  61.     imPixelSize.width = [theImage pixelsWide];
  62.     imPixelSize.height = [theImage pixelsHigh];
  63.  
  64.     if (NX_ALTERNATEMASK &  event->flags) {
  65.     doAlternateDrag = YES;
  66.     altDragSize = faceTiffSize;
  67.     maxBound.x = imSize.width;
  68.     maxBound.y = imSize.height - altDragSize;
  69.     [self convertPoint:&maxBound fromView:nil];
  70.     }
  71.         
  72.     oldMask = [window addToEventMask:NX_MOUSEDRAGGEDMASK|NX_MOUSEUPMASK];
  73.     if( !crossCursor ) {
  74.     crossCursor = [[NXCursor alloc] initFromImage:
  75.               [[NXImage alloc] initFromSection:"cross.tiff"]];
  76.     [crossCursor setHotSpot:&hot];
  77.     }
  78.     [crossCursor push];
  79.     p = start = event->location;
  80.     [self convertPoint:&start fromView:nil];
  81.     last = start;
  82.  
  83.     [self lockFocus];
  84.        
  85.     PSsetlinewidth(0.0);
  86.     PSsetinstance(YES);
  87.     event = [NXApp getNextEvent:DRAG_MASK];
  88.        
  89.     while( event->type != NX_MOUSEUP ) {
  90.     p = event->location;
  91.     [self convertPoint:&p fromView:nil];
  92.     PSnewinstance();
  93.     if( p.x != last.x || p.y != last.y ) {
  94.         PSsetinstance(YES);
  95.         PSsetgray(1.0);
  96.         if ( doAlternateDrag ) {
  97.         start = p;
  98.         if ( start.x < altDragSize ) start.x = altDragSize;
  99.         if ( start.y < 0 ) start.y = 0;
  100.         if ( start.x > maxBound.x ) start.x = maxBound.x;
  101.         if ( start.y > maxBound.y ) start.y = maxBound.y;
  102.         PSrectstroke(start.x, start.y,
  103.                  - (altDragSize + 1), (altDragSize + 1) );
  104.         }
  105.         else {
  106.         PSrectstroke(start.x, start.y,
  107.                  (p.x - start.x),
  108.                  (p.y - start.y));
  109.         }
  110.         PSsetinstance(NO);
  111.         last = p;
  112.         NXPing();
  113.         }
  114.     p = event->location;
  115.     event = [NXApp getNextEvent:DRAG_MASK];
  116.     }
  117.         
  118.     [self convertPoint:&p fromView:nil];
  119.     selectRect.origin.x = start.x; selectRect.origin.y = start.y;
  120.     if ( doAlternateDrag ) {
  121.     selectRect.size.width  = -altDragSize;
  122.     selectRect.size.height = altDragSize;
  123.     }
  124.     else {
  125.     selectRect.size.width  = p.x - start.x; 
  126.     selectRect.size.height = p.y - start.y;
  127.     }
  128.  
  129.     [self unlockFocus];
  130.     [window flushWindow];
  131.     [window setEventMask:oldMask];
  132.     [crossCursor pop];
  133.  
  134.     return self;
  135. }
  136.  
  137. - copy:sender
  138. {
  139.     NXStream    *st;
  140.     NXBitmapImageRep *bm;
  141.     int        i = 0;
  142.     const char    *types[2];
  143.     id        pb = [Pasteboard new];
  144.     
  145.     //    note that "owner:" in the following can not be "self", or the
  146.     //    id of anything else which might be freed inbetween "Copy"
  147.     //      operations.
  148.     types[i++] = NXTIFFPboardType;
  149.     [pb declareTypes:types num:i owner:NXApp];
  150.  
  151.     [self lockFocus];
  152.     bm = [[NXBitmapImageRep alloc] initData:NULL fromRect:&selectRect];
  153.     [self unlockFocus];
  154.  
  155.     st = NXOpenMemory(NULL, 0, NX_WRITEONLY);
  156.     [bm writeTIFF:st];
  157.     [pb writeType:NXTIFFPboardType fromStream:st];
  158.     NXCloseMemory(st, NX_FREEBUFFER);
  159.     [bm free];
  160.  
  161.     return self;
  162. }
  163.  
  164. - paste:sender
  165. {
  166.     NXStream     *st;
  167.     char    tmpName[40];
  168.     int        pasteCount;
  169.     id      pb;
  170.  
  171.     pb = [Pasteboard new];    
  172.     (void)[pb types];
  173.     st = [pb readTypeToStream:NXTIFFPboardType];
  174.     
  175.     pasteCount = [[NXApp delegate] nextPasteCounter];
  176.     sprintf(tmpName, "%d_PasteImage", pasteCount);
  177.     
  178.     [[window delegate]
  179.             displayImage: [[NXBitmapImageRep alloc] initFromStream:st]
  180.         withName:tmpName  atResolution:0];
  181.     NXCloseMemory(st, NX_FREEBUFFER);
  182.  
  183.     return self;
  184. }
  185.  
  186. @end